package com.hkc.huakang.hkcpaperless;

import android.content.Context;

import com.hkc.huakang.hkcpaperless.bin.ChannelList;
import com.hkc.huakang.hkcpaperless.bin.DrugInfo;
import com.hkc.huakang.hkcpaperless.bin.LoggedInUser;
import com.hkc.huakang.hkcpaperless.bin.OrderDetail;
import com.hkc.huakang.hkcpaperless.bin.OutOrderDetail;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MysqlConnectUtil {
    Connection connection = null;
    Statement statement = null;
    boolean isLogin = false;

    //    String IPAdress="192.168.168.100";
//    String dataBaseName="hkcdb2";
//    String username="root";
//    String pwd="qq1223";
//    String IPAdress="10.1.100.45";
//    String dataBaseName="腕表显示测试数据库";
//    String username="root";
//    String pwd="qq1223";
    public void connect(ConnectResult connectResult, Context context) {

        String IPAdress = SharedPreferencesUtils.getInstance().getMysqlIP(context);
        String dataBaseName = SharedPreferencesUtils.getInstance().getDataName(context);
        String username = SharedPreferencesUtils.getInstance().getMYSQL_UserNAME(context);
        String pwd = SharedPreferencesUtils.getInstance().getMYSQL_PASS(context);
        new Thread(new Runnable() {
            @Override
            public void run() {

                final String CLS = "com.mysql.jdbc.Driver";
                // final String CLS = "com.mysql.cj.jdbc.Driver";
                //final String URL = "jdbc:mysql://194.2.1.101/hkcdb2";
                //final String URL = "jdbc:mysql://10.1.100.45/wuhe";
                final String URL = "jdbc:mysql://" + IPAdress + "/" + dataBaseName + "?useSSL=false";
                //final String USER = "root";
                //final String PWD = "qq1223";

                try {
                    Class.forName(CLS);
                    DriverManager.setLoginTimeout(5);
                    connection = DriverManager.getConnection(URL, username, pwd);
                    connectResult.onConnectSuccess();

                } catch (Exception e) {
                    connectResult.onConnectFail(e);
                }
            }
        }).start();
    }

    public void onClose() {
        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface ConnectResult {
        public void onConnectFail(Exception e);

        public void onConnectSuccess();
    }

    public LoggedInUser getUser(String sql) {
        ResultSet resultSet = null;
        try {
            if (connection == null) {
                return null;
            } else {
                statement = connection.createStatement();
                resultSet = statement.executeQuery(sql);
                resultSet.last();
                int size = 0;

                size = resultSet.getRow();
                //为了能够继续遍历resultSet集合，我们需将指针移动至第一个元素
                resultSet.beforeFirst();
                LoggedInUser loggedInUser = new LoggedInUser();
                while (resultSet.next()) {
                    loggedInUser.setUserId(resultSet.getString("user_id"));
                    loggedInUser.setDisplayName(resultSet.getString("user_name"));
                    loggedInUser.setPassword(resultSet.getString("password"));
                    loggedInUser.setNickName(resultSet.getString("nick_name"));
                }
                onClose();
                return loggedInUser;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<OrderDetail> getDisposeOrder(String sql) {
        ResultSet resultSet = null;
        try {
            if (connection == null) {
                return null;
            }

            statement = connection.createStatement();

            resultSet = statement.executeQuery(sql);
            resultSet.last();
            int size = 0;

            size = resultSet.getRow();

            //为了能够继续遍历resultSet集合，我们需将指针移动至第一个元素
            resultSet.beforeFirst();
            List<OrderDetail> disposeOrders = new ArrayList<OrderDetail>();
            while (resultSet.next()) {
                OrderDetail disposeOrder = new OrderDetail();
                disposeOrder.setP_name(resultSet.getString("p_name"));
                disposeOrder.setSex(resultSet.getString("sex"));
                disposeOrder.setAge(resultSet.getString("age"));
                disposeOrder.setOrder_no(resultSet.getString("order_no"));
                disposeOrder.setIn_time(resultSet.getString("in_time"));
                disposeOrder.setDisease(resultSet.getString("disease"));
                disposeOrder.setDrugId(resultSet.getString("drug_id"));
                disposeOrder.setQuantity(resultSet.getString("quantity"));
                disposeOrder.setUnit_convercoef(resultSet.getString("unit_convercoef"));
                disposeOrder.setOrder_unit(resultSet.getString("order_unit"));
                disposeOrder.setBig_unit(resultSet.getString("big_unit"));
                disposeOrder.setSmall_unit(resultSet.getString("small_unit"));
                disposeOrder.setDrug_name(resultSet.getString("drug_name"));
                disposeOrder.setDrug_spec(resultSet.getString("drug_spec"));
                disposeOrder.setOrder_group_no(resultSet.getString("order_group_no"));
                disposeOrder.setBasket_card(resultSet.getInt("basket_card"));
                disposeOrder.setPatient_id(resultSet.getString("patient_id"));
                disposeOrder.setWin_no(resultSet.getString("win_no"));
                disposeOrder.setBack_stock_pos(resultSet.getString("back_stock_pos"));
                disposeOrders.add(disposeOrder);


            }
            return disposeOrders;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public OutOrderDetail getOutOrderDetail(String sql) {
        ResultSet resultSet = null;
        try {
            if (connection == null) {
                return null;
            }

            statement = connection.createStatement();

            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            resultSet.last();
            int size = 0;

            size = resultSet.getRow();

            OutOrderDetail orderDetail = null;
            if (size > 0) {
                orderDetail = new OutOrderDetail();
                resultSet.beforeFirst();
                while (resultSet.next()) {
                    orderDetail.setMachine_id(resultSet.getString("machine_id"));
                    orderDetail.setRow_no(resultSet.getInt("row_no"));
                    orderDetail.setCol_no(resultSet.getInt("col_no"));
                    orderDetail.setMachineName(resultSet.getString("machine_name"));
                    orderDetail.setReal_quantity(resultSet.getString("real_quantity"));
                    orderDetail.setSerial_level(resultSet.getInt("serial_level"));

                }
            }

            return orderDetail;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public ChannelList searchGrugPosition(String sql) {
        ResultSet resultSet = null;
        try {
            if (connection == null) {
                return null;
            }

            statement = connection.createStatement();

            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            resultSet.last();
            int size = 0;

            size = resultSet.getRow();
           // System.out.println("size:" + size);
            ChannelList channelList = null;
            if (size > 0) {
                channelList = new ChannelList();
                resultSet.beforeFirst();
                while (resultSet.next()) {
                    channelList.setMachine_id(resultSet.getString("machine_id"));
                    channelList.setRow_no(resultSet.getInt("row_no"));
                    channelList.setMachineName(resultSet.getString("machine_name"));
                    channelList.setCol_no(resultSet.getInt("col_no"));
                    channelList.setSerial_level(resultSet.getInt("serial_level"));
                    channelList.setOutpos_no(resultSet.getString("outpos_no"));

                }
            }

            return channelList;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //    public  List<OrderDetail> getOrderDetails(String sql) {
//        ResultSet resultSet = null;
//        try {
//            if (connection == null) {
//                return null;
//            } else {
//                statement = connection.createStatement();
//                resultSet = statement.executeQuery(sql);
//                resultSet.last();
//                int size = 0;
//
//                size = resultSet.getRow();
//                System.out.println("size:" + size);
//                //为了能够继续遍历resultSet集合，我们需将指针移动至第一个元素
//                resultSet.beforeFirst();
//                List<OrderDetail> orderDetails=new ArrayList<OrderDetail>();
//                while (resultSet.next()) {
//                    OrderDetail orderDetail = new OrderDetail();
//                    orderDetail.setOrder_id(resultSet.getString("order_id"));
//                    orderDetail.setOrder_unit(resultSet.getString("order_unit"));
//                    orderDetail.setOrder_no(resultSet.getString("order_no"));
//                    orderDetail.setQuantity(resultSet.getString("quantity"));
//                    orderDetail.setDrug_name(resultSet.getString("drug_name"));
//                    orderDetail.setDrug_spec(resultSet.getString("drug_spec"));
//                    orderDetail.setManufactory(resultSet.getString("manufactory"));
//                    orderDetails.add(orderDetail);
//                }
//                onClose();
//                return orderDetails;
//            }
//
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }
    public List<DrugInfo> getSuperVersionCode(String sql) {
        ResultSet resultSet = null;
        try {
            if (connection == null) {
                return null;
            } else {
                statement = connection.createStatement();
                resultSet = statement.executeQuery(sql);
                resultSet.last();
                int size = 0;

                size = resultSet.getRow();

                //为了能够继续遍历resultSet集合，我们需将指针移动至第一个元素
                resultSet.beforeFirst();
                List<DrugInfo> list = new ArrayList<>();
                while (resultSet.next()) {
                    DrugInfo drugInfo = new DrugInfo();
                    drugInfo.setDrug_id(resultSet.getString("drug_id"));
                    drugInfo.setDrug_name(resultSet.getString("drug_name"));
                    drugInfo.setDrug_spec(resultSet.getString("drug_spec"));
                    drugInfo.setManufactory(resultSet.getString("manufactory"));
                    drugInfo.setSupervision_code(resultSet.getString("supervision_code"));
                    list.add(drugInfo);
                }
                onClose();
                return list;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public  boolean  insertOutMachineDetails(String sql){
        boolean  res;
        try {
            if (connection == null) {
                return false;
            } else {
                boolean  isSendSuccess;
                statement = connection.createStatement();
                statement.execute(sql);
                isSendSuccess=  statement.getUpdateCount()>0?true:false;
                onClose();
                return  isSendSuccess;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;
    }

}

